package online.shenjian.redis.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.codec.CompositeCodec;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.redisson.config.TransportMode;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author shenjian
 * @since 2022/7/18
 */

@Component
@ConditionalOnProperty(name = "spring.redis.enable", matchIfMissing = true)
@Configuration
public class RedissonConfig {

    @Bean(name = "stringLongCodec")
    public CompositeCodec stringLongCodec() {
        return new CompositeCodec(new org.redisson.client.codec.StringCodec(), new org.redisson.client.codec.LongCodec());
    }

    @Bean(name = "stringCodec")
    public StringCodec stringCodec() {
        return new org.redisson.client.codec.StringCodec();
    }

    @Bean("redissonClient")
    public RedissonClient redissonClient() {
        Config config = new Config();
        config.setTransportMode(TransportMode.NIO);
        SingleServerConfig singleServerConfig = config.useSingleServer();
        singleServerConfig.setAddress("redis://127.0.0.1:6379");
        singleServerConfig.setPassword("123456");
        singleServerConfig.setTimeout(2000);
        singleServerConfig.setDatabase(0);
        singleServerConfig.setConnectionPoolSize(1000);
        singleServerConfig.setConnectionMinimumIdleSize(2);
        RedissonClient redisson = Redisson.create(config);
        return redisson;
    }
}
